package com.jic.product.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.manage.request.CategoryManagementRequest;
import com.jic.product.manage.response.CategoryManagementResponse;
import com.jic.product.service.CategoryManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by HeJun on 2020/1/13
 *
 * @Description:
 */
@Api(value="商家类目资质管理controller", tags = "商家类目资质管理", description = "CategoryManagementController")
@RestController
@RequestMapping("/categoryManagement")
@Slf4j
public class CategoryManagementController  {

    @Autowired
    CategoryManagementService categoryManagementService;

    @ApiOperation(value="获取类目资质信息", notes="获取类目资质信息")
    @PostMapping("/queryPage")
    public RestResult<PageResult<CategoryManagementResponse>> queryPage(@RequestBody CategoryManagementRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        CategoryManagementRequest categoryManagement = BeanUtil.copy(form, CategoryManagementRequest.class);
        PageResult<CategoryManagementResponse> result = categoryManagementService.queryPage(categoryManagement, page);
        List<CategoryManagementResponse> categoryResponses = result.getRows().stream().map(temp -> {
            CategoryManagementResponse cmr = BeanUtil.copy(temp, CategoryManagementResponse.class);
            return cmr;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<CategoryManagementResponse> pageResult = new PageResult<>();
        pageResult.setRows(categoryResponses);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value="提供给商家类目资质信息", notes="提供给商家类目资质信息")
    @PostMapping("/query")
    public RestResult<PageResult<CategoryManagementResponse>> query(@RequestBody CategoryManagementRequest categoryManagementRequest) {
        PageResult<CategoryManagementResponse> result = categoryManagementService.query(categoryManagementRequest);

        List<CategoryManagementResponse> categoryResponses = result.getRows().stream().map(temp -> {
            CategoryManagementResponse cmr = BeanUtil.copy(temp, CategoryManagementResponse.class);
            return cmr;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<CategoryManagementResponse> pageResult = new PageResult<>();
        pageResult.setRows(categoryResponses);
        pageResult.setTotal(0);
        return RestResult.success(pageResult);
    }

    @ApiOperation(value="新增类目资质信息", notes="新增类目资质信息")
    @PostMapping("/insert")
    public RestResult<Long> insert(@RequestBody CategoryManagementRequest category) {
        category.setDeptId(category.getAdminOperate().getDeptId());
        CategoryManagementRequest categoryQuery = new CategoryManagementRequest();
        long result = 0;
        category.setStatus(1);
        categoryQuery.setDeptId(category.getDeptId());
        if (null != category.getIds()) {
            String[] ids = category.getIds().split(",");
            String[] categoryCode = category.getCategoryCode().split(",");
            String[] categoryName = category.getCategoryName().split(",");
            try {

                for (int i = 0; i < ids.length; i++) {
                    category.setCategoryId(Long.parseLong(ids[i]));
                    category.setCategoryCode(categoryCode[i]);
                    category.setCategoryName(categoryName[i]);
                    RestResult resultInfo=categoryManagementService.queryInfo(category);
                    if (null == resultInfo.getData()){
                        result = categoryManagementService.insert(category);
                    }else{
                        return RestResult.error("9999", "类目不可重复，请检查！");
                    }

                }
            } catch (Exception e) {
                log.error("error",e);
                return RestResult.error("9999", e.getLocalizedMessage());
            }

        }
        return RestResult.success(result);
    }

    @ApiOperation(value="修改类目资质信息", notes="修改类目资质信息")
    @PostMapping("/update")
    public RestResult<Long> update(@RequestBody CategoryManagementRequest t) {
        long result = 0;
        if (StringUtils.isNotEmpty(t.getIds())) {
            String[] ids = t.getIds().split(",");
            for (int i = 0; i < ids.length; i++) {
                t.setId(Long.parseLong(ids[i]));
                try {
                    result = categoryManagementService.update(t);
                } catch (Exception e) {
                    e.printStackTrace();
                    return RestResult.error("9999", e.getLocalizedMessage());
                }
            }
            return RestResult.success(result);
        }
        try {
            result = categoryManagementService.update(t);
        } catch (Exception e) {
            log.error("error",e);
            return RestResult.error("9999", e.getLocalizedMessage());
        }
        return RestResult.success(result);
    }

    public RestResult<Long> delete(long id) {
        return null;
    }

    @ApiOperation(value="删除类目资质信息", notes="删除类目资质信息")
    @PostMapping("/delete")
    public RestResult<Long> delete(@RequestBody CategoryManagementRequest category) {
        long result = 0;
        try {
            if (category !=null){
                result = categoryManagementService.delete(category.getId());
            }
        } catch (Exception e) {
            log.error("error",e);
            return RestResult.error("9999", e.getLocalizedMessage());
        }
        return RestResult.success(result);
    }
}
